<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"          			
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      >
    <ui:composition template="TaskProLayout.xhtml">
        <ui:define name="content">
            <!--Students dataTable START -->
            <div class="Post">
                <h:form id="studentsForm">  
                    <p:dataTable  var="student" value="#{kantorTaskBean.kantorStudents}"  
                                  selection="#{kantorTaskBean.selectedStudents}" 
                                  emptyMessage="No students were find by specific criteria"
                                  paginator="true" rows="10" 
                                  selectionMode="multiple"
                                  rowKey="#{student.id}" paginatorAlwaysVisible="false"
                                  >  
                        <f:facet name="header">  
                            Select students
                        </f:facet>  

                        <p:column>  
                            <f:facet name="header">  
                                <h:outputText value="Login" />  
                            </f:facet>  
                            <h:outputText value="#{student.login}" >
                                <f:converter converterId="StringConverter" />
                            </h:outputText>
                        </p:column> 
                        <p:column>  
                            <f:facet name="header">  
                                <h:outputText value="Name" />  
                            </f:facet>  
                            <h:outputText value="#{student.name}" />  
                        </p:column>  

                        <p:column>  
                            <f:facet name="header">  
                                <h:outputText value="Surname" />  
                            </f:facet>  
                            <h:outputText value="#{student.surname}" />  
                        </p:column> 
                        <p:column>  
                            <f:facet name="header">  
                                <h:outputText value="E-mail" />  
                            </f:facet>  
                            <h:outputText value="#{student.email}" >
                                <f:converter converterId="StringConverter" />
                            </h:outputText>
                        </p:column> 
                        <f:facet name="footer">  
                            <p:commandButton value="Display task" action="#{kantorTaskBean.showTaskByStudents()}" ajax="true" update=":taskForm"/> 
                            <p:commandButton value="Choose all" action="#{kantorTaskBean.chooseAllStundents()}" ajax="true" update=":studentsForm"/>
                        </f:facet>  
                    </p:dataTable>  
                    <h:outputText value="Multiple select or unselect use CTR or SHIFT"/>
                </h:form>
            </div>
            <!--Students dataTable END -->
            <!--Task filter START -->
            <div class="Post">
                <p:panel  id="pnl"  header="Filter" toggleable="true" collapsed="true"  toggleSpeed="100" closeSpeed="100" widgetVar="panel">  
                    <h:form>
                        <h:panelGrid id="filter" columns="2" cellpadding="9" title="Filter">  
                            <h:outputText value="State of task" />
                            <p:selectCheckboxMenu value="#{kantorTaskBean.selectedStates}" label="State" >  
                                <f:selectItems value="#{kantorTaskBean.statesOfTask}" />  
                            </p:selectCheckboxMenu>  
                        </h:panelGrid>
                        <p:commandButton value="Apply filter" action="#{kantorTaskBean.showTaskByStudents()}" ajax="true" update=":taskForm"/> 
                    </h:form>
                </p:panel>  
            </div>
            <!--Task filter END -->
            <!--Submitted task after satisfy filtre START -->
            <div class="Post">
                <h:form id="taskForm">  
                    <p:dataTable rendered="#{kantorTaskBean.displaySubTaskForm}" var="task" value="#{kantorTaskBean.submitTasks}"  
                                 selection="#{kantorTaskBean.submitTaskChoosen}" selectionMode="single" paginatorAlwaysVisible="false"
                                 styleClass="facesFont"
                                 emptyMessage="No task was founded"
                                 paginator="true" rows="10"
                                 rowKey="#{task.id}">  
                        <p:ajax event="rowSelect"  
                                update=":taskDetail:display"  oncomplete="taskDialog.show()"/>          
                        <f:facet name="header">  
                            <h:outputText value="Submitted task by students" style="font-size: 12px"/>
                        </f:facet>  
                        <p:column headerText="Label">                   
                            <h:outputText value="#{task.assignment.template.name}" />  
                        </p:column>  
                        <p:column headerText="Submit">  
                            <h:outputText value="#{task.dateOfSubmit}" >
                                <f:convertDateTime type="both" timeZone="Europe/Prague"></f:convertDateTime>
                            </h:outputText>
                        </p:column>  
                        <p:column headerText="Deadline">  
                            <h:outputText value="#{task.assignment.deadline}" >
                                <f:convertDateTime type="both" timeZone="Europe/Prague"></f:convertDateTime>
                            </h:outputText>
                        </p:column>  
                        <p:column headerText="State">  
                            <h:outputText value="#{task.stateOfTask}" >                     
                            </h:outputText>
                        </p:column>  
                        <p:column headerText="Type">                  
                            <h:outputText value="#{task.assignment.type}" />
                        </p:column>  
                        <p:column headerText="Student">   
                            <h:outputText value="#{task.student.surname} #{task.student.name}" >                   
                            </h:outputText>
                        </p:column>  
                        <p:column headerText="Login">   
                            <h:outputText value="#{task.student.login}" >   
                                <f:converter converterId="StringConverter" />
                            </h:outputText>
                        </p:column>  
                        <p:column headerText="Subject">  
                            <h:outputText value="#{task.assignment.subject.code}" >                     
                            </h:outputText>
                        </p:column>  
                        <p:column headerText="Grade">  
                            <h:outputText value="#{task.grade}" >                     
                            </h:outputText>
                        </p:column> 
                        <f:facet name="footer" >
                            <h:outputText value=" " />
                        </f:facet>
                    </p:dataTable>  
                </h:form>
            </div>
            <!--Submitted task after satisfy filtre END -->

            <!-- Selected task detail dialog START -->
            <p:dialog header="Task detail: #{kantorTaskBean.submitTaskChoosen.assignment.template.name}" widgetVar="taskDialog" resizable="false" 
                      width="900" height="600"  modal="true" appendToBody="true"
                      dynamic="true" >  
                <h:form id="taskDetail">
                    <p:messages  id="messageVersion" showDetail="true" autoUpdate="true"/>   

                    <h:panelGrid id="display" columns="2" cellpadding="4" title="Assigned Task">  
                        <h:outputText value="Label"/>
                        <h:outputText value="#{kantorTaskBean.submitTaskChoosen.assignment.template.name}" />
                        <h:outputText value="Description"/>
                        <h:outputText value="#{kantorTaskBean.submitTaskChoosen.assignment.template.description}" escape="false" >
                           <f:converter converterId="StringFormatter"/>
                        </h:outputText>
                        <h:outputText value="Subject"/>
                        <h:outputText value="#{kantorTaskBean.submitTaskChoosen.assignment.subject.code}" />
                        <h:outputText value="Last submitted  student"/>
                        <h:outputText value="#{kantorTaskBean.submitTaskChoosen.student.name} #{kantorTaskBean.submitTaskChoosen.student.surname}" />
                        <h:outputText value="Date of assign:"/>
                        <h:outputText value="#{kantorTaskBean.submitTaskChoosen.assignment.dateOfAssignment}" >
                            <f:convertDateTime type="both" timeZone="Europe/Prague"></f:convertDateTime>
                        </h:outputText>
                        <h:outputText value="Deadline"/>
                        <h:outputText value="#{kantorTaskBean.submitTaskChoosen.assignment.deadline}" >
                            <f:convertDateTime type="both" timeZone="Europe/Prague"></f:convertDateTime>
                        </h:outputText>            
                        <h:outputText value="Date of last submit:"/>
                        <h:outputText value="#{kantorTaskBean.submitTaskChoosen.dateOfSubmit}" >
                            <f:convertDateTime type="both" timeZone="Europe/Prague"></f:convertDateTime>
                        </h:outputText>
                        <h:outputText value="Template files" />
                        <p:commandButton value="Template file"  ajax="true"  onclick="fileDialog.show();" update=":templateFiles"/> 
                        <h:outputText value="Files" />
                        <p:commandButton value="Files with task" action="#{kantorTaskBean.loadVersions()}" ajax="true"
                                         onclick="dlg1.show();" update=":dialogFiles"/> 
                        <h:outputText value="Evaluation" />
                        <h:inputTextarea style="width:300px;height:100px;"  value="#{kantorTaskBean.submitTaskChoosen.evaluation}" />
                        <h:outputText value="Grade:" />
                        <h:inputText  required="true"  value="#{kantorTaskBean.submitTaskChoosen.grade}" />
                        <h:outputText value="State of task" />
                        <h:selectOneMenu id="statusTask" value="#{kantorTaskBean.submitTaskChoosen.stateOfTask}" > 
                            <f:selectItem itemValue="#{kantorTaskBean.submitTaskChoosen.stateOfTask}" itemLabel="Choose state" />
                            <f:selectItem itemValue="ACCEPTED" itemLabel="Accepted" />
                            <f:selectItem itemValue="REJECTED" itemLabel="Rejected" />
                        </h:selectOneMenu>
                    </h:panelGrid>  
                    <p:commandButton value="Mark task" update=":taskForm" action="#{kantorTaskBean.rateTask()}" 
                                     ajax="true" oncomplete="taskDialog.show()"></p:commandButton>
                    <p:commandButton value="Close" 
                                     ajax="true" oncomplete="taskDialog.hide();"></p:commandButton>
                </h:form>
            </p:dialog>   
            <!-- Selected task detail dialog END -->

            <!-- Submitted files with selected task dialog START -->
            <p:dialog width="870" height="400" header="Submitted with task: #{kantorTaskBean.submitTaskChoosen.assignment.template.name}"
                      widgetVar="dlg1" modal="true" dynamic="true" appendToBody="true"
                      >
                <h:form id="dialogFiles">
                    <p:dataTable var="taskFile" value="#{kantorTaskBean.versions}" id="dialogFiles"
                                 rowKey="#{taskFile.id}" paginator="true" 
                                 paginatorAlwaysVisible="false" rows="10" >  
                        <p:column headerText="File" >  
                            <p:commandLink value="#{taskFile.fileName}" ajax="false">  
                                <p:fileDownload value="#{kantorTaskBean.downloadFilesByTask(taskFile)}" />  
                            </p:commandLink> 
                        </p:column>
                        <p:column headerText="Size" >  
                            <h:outputText title="size" value="#{taskFile.fileSize}"  />  
                        </p:column>
                        <p:column headerText="Description">  
                            <h:outputText value="#{taskFile.description}" />  
                        </p:column>
                        <p:column headerText="Evaluation">  
                            <p:cellEditor>  
                                <f:facet name="output">  
                                    <h:outputText value="#{taskFile.evaluation}" >
                                        <f:converter converterId="StringConverter" />
                                    </h:outputText>
                                </f:facet>  
                                <f:facet name="input">  
                                    <p:inputTextarea value="#{taskFile.evaluation}" style="width:100%"/>  
                                </f:facet>  
                            </p:cellEditor>  
                        </p:column>
                        <p:column headerText="Date of submit">  
                            <h:outputText value="#{taskFile.version.dateOfSubmit}" >
                                <f:convertDateTime type="both" timeZone="Europe/Prague"></f:convertDateTime>
                            </h:outputText>
                        </p:column>    
                        <p:column headerText="Version">  
                            <h:outputText value="#{taskFile.version.numberOfVersion}" />  
                        </p:column>
                        <p:column headerText="Sender">  
                            <h:outputText value="#{taskFile.version.submittedStudent.name} #{taskFile.version.submittedStudent.surname}" />  
                        </p:column>
                        <p:column headerText="Option">
                            <p:rowEditor />  
                        </p:column>
                        <p:column headerText="Download">  
                            <p:commandButton value="Download" ajax="false">  
                                <p:fileDownload value="#{kantorTaskBean.downloadFilesByTask(taskFile)}" />  
                            </p:commandButton> 
                        </p:column> 
                    </p:dataTable>
                    <p:commandButton value="Save" oncomplete="dlg1.hide() , taskDialog.show()" update=":taskDetail" action="#{kantorTaskBean.saveEvaluationOfVersion()}" ajax="true"/>
                    <p:commandButton value="Close" oncomplete="dlg1.hide()" ajax="true"/>
                </h:form>
            </p:dialog>
            <!-- Submitted files with selected task dialog END -->

            <!-- Template files with selected task dialog START -->
            <p:dialog width="870" height="400" header="Template files with task: #{kantorTaskBean.submitTaskChoosen.assignment.template.name}"
                      widgetVar="fileDialog" modal="true" dynamic="true" appendToBody="true"
                      >
                <h:form  id="templateFiles">
                    <p:dataTable var="templateFile" value="#{kantorTaskBean.submitTaskChoosen.assignment.template.templateFiles}"
                                 rowKey="#{templateFile.id}" paginator="true" 
                                 paginatorAlwaysVisible="false" rows="10" >  
                        <p:column headerText="File" >  
                            <h:outputText title="file" value="#{templateFile.fileName}"  />  
                        </p:column>
                        <p:column headerText="File size" >  
                            <h:outputText title="file" value="#{templateFile.fileSize}"  />  
                        </p:column>                   
                        <p:column headerText="Download">  
                            <p:commandButton value="Download" ajax="false">  
                                <p:fileDownload value="#{studentAsSubmitBean.download(templateFile)}" />  
                            </p:commandButton> 
                        </p:column>                    
                    </p:dataTable>
                    <p:commandButton value="Close" oncomplete="fileDialog.hide()"  ajax="true"/>
                </h:form>
            </p:dialog>
            <!-- Template files with selected task dialog END -->
        </ui:define>
    </ui:composition>
</html>